#include "globalmanager.h"


void globalmanager::run()
{
    while(1)
    {
        //展示欢迎界面
        welcome();

        //输入账号密码
        inputAccountID();
        if (!inputPassword())
            continue;
        else
        {
            //选择账户操作
            while(1)
            {
                show_menu();

                int MenuChoice;
                std::cin>>MenuChoice;

                if (MenuChoice==1)
                    getBalance();

                else if (MenuChoice==2)
                    withdraw();

                else if (MenuChoice==3)
                    deposit();

                else if (MenuChoice==4)
                    setPassword();

                else if (MenuChoice==5)
                    transfer();

                else if (MenuChoice==6)
                {
                    cardout();
                    break;
                }

                else
                    std::cout<<"Please enter the true choice!"<<std::endl;
            }
        }
    }
}

void globalmanager::show_menu()
{
    std::cout<<std::endl;
    std::cout<<"1: View my balance"<<std::endl;
    std::cout<<"2: Withdraw cash"<<std::endl;
    std::cout<<"3: Deposit funds"<<std::endl;
    std::cout<<"4: Reset password"<<std::endl;
    std::cout<<"5: Transfer money"<<std::endl;
    std::cout<<"6: Card out"<<std::endl;

    std::cout<<"Please enter a choice:";
}

void globalmanager::welcome()
{
    std::cout<<"Welcome to the ATM machine!"<<std::endl;
}

void globalmanager::deposit()
{
    std::cout<<"How much do you want to deposit?"<<std::endl;
    int money;
    std::cin>>money;

    machine.deposit(money);
    std::cout<<"Deposit successfully!"<<std::endl;
}

void globalmanager::withdraw()
{
    std::cout<<"How much do you want to withdraw?"<<std::endl;
    int money;
    std::cin>>money;
    int pp=machine.withdraw(money);
    if (pp==0)
        std::cout<<"Withdraw successfully!"<<std::endl;
    else if (pp==-1)
        std::cout<<"Your balance is too low to withdraw!"<<std::endl;
    else if (pp==1)
        std::cout<<"You can only withdraw with multiples of 100!"<<std::endl;
    else if (pp==2)
        std::cout<<"You can only withdraw 20000 one time!"<<std::endl;
    else if (pp==3)
        std::cout<<"You can only withdraw 100000 one day!"<<std::endl;
}

void globalmanager::setPassword()
{
    std::string newPassword;
    std::string verify;

    std::cout<<"Please input your new password."<<std::endl;
    std::cin>>newPassword;

    std::cout<<"Please input password again."<<std::endl;
    std::cin>>verify;

    if (newPassword!=verify)
        std::cout<<"Two inputs are not same!"<<std::endl;
    else
    {
        if (machine.setPassword(newPassword))
            std::cout<<"Reset successfully!"<<std::endl;
        else
            std::cout<<"The password must be six numbers!"<<std::endl;
    }
}

void globalmanager::transfer()
{
    std::string account;
    std::string verify;

    std::cout<<"Which account do you want to transfer?"<<std::endl;
    std::cin>>account;
    std::cout<<"Please input the account again."<<std::endl;
    std::cin>>verify;

    if (account!=verify)
    {
        std::cout<<"Two inputs are not same!"<<std::endl;
    }
    else
    {
        std::cout<<"How much money do you want to transfer?"<<std::endl;
        int money;
        std::cin>>money;

        int a=machine.transfer(account,money);
        if (a==0)
            std::cout<<"Transfer successfully!"<<std::endl;
        else if (a==-1)
            std::cout<<"Your balance is too low to transfer!"<<std::endl;
        else if (a==1)
            std::cout<<"Please input the correct AccoutID!"<<std::endl;
    }
}

void globalmanager::getBalance()
{
    std::cout<<"Your balance is: ";
    std::cout<<machine.getBalance();
    std::cout<<"RMB"<<std::endl;
}

void globalmanager::inputAccountID()
{
    std::string id;
    std::cout<<"Please input your AccoutID."<<std::endl;
    while(1)
    {
        std::cin>>id;
        if (!machine.SetAccountID(id))              //如果没有设置成功即代表没有这个账户
        {
            std::cout<<"Please input the correct AccoutID!"<<std::endl;
            continue;
        }
        else
            break;
    }
}

bool globalmanager::inputPassword()
{

        while (1)
        {
            if (machine.IfLocked())
            {
                std::cout<<"You have input wrong password in three times."
                           " Your account is locked!"<<std::endl;
                return false;
            }

            std::cout<<"Please input the password!"<<std::endl;
            std::string password;
            std::cin>>password;
            int n=machine.IfTrue(password);
            if(n>0&&n<3)
            {
                std::cout<<"Please input the correct password!\n"
                           "If you input wrong password in three times, "
                           "your account will be locked!"<<std::endl;
                continue;
            }
            else if (n==0)
                return true;
        }
}

void globalmanager::cardout()               
{
    machine.save();                 //退卡前再保存一次，以免出错
    std::cout<<"Welcome to come again!"<<std::endl;      
    std::cout<<std::endl;           //与下一位用户分隔开
}
